package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;


@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements  UserService {
    @Autowired
    RoleDao roleDao;

    @Autowired
    UserDao  userDao;
    @Autowired
    PermissionDao permissionDao;

    @Override
    public User loadUserByUsername(String username) {
         //根据用户名称查询用户的基本信息(要返回id username password)
          User user=userDao.findByUserName(username);
        if (user !=null){
            //根据用户的id查询用户的角色集合(t_user t_user_role t_role)
           Set<Role> roles=roleDao.findUserById(user.getId());
            //循环角色查询角色下面的权限
            for (Role role : roles) {
                Set<Permission>  permissions=permissionDao.findByRoleId(role.getId());
                role.setPermissions(permissions);
            }
            user.setRoles(roles);
        }
        return user;
    }
}
